//
//  SFCAuthTests.m
//  SVNForCocoa
//
//  Created by Jeremy Pereira on 17/11/2013.
//  Copyright (c) 2013 Jeremy Pereira. All rights reserved.
//
/*!
 *    @file
 *    @brief Authentication tests
 */

#import <XCTest/XCTest.h>
#import "SFCFileSetUpHelper.h"
#import "SFCClient.h"
#import "SFCTestAuthProvider.h"

/*!
 *    @brief Authentication tests.
 *
 *    Tests various authentication methods.
 */
@interface SFCAuthTests : XCTestCase

@end

/*
 *  For these tests, we need a server as well as the repo and working copies
 */
@implementation SFCAuthTests
{
	SFCFileSetUpHelper* fileHelper;
    SFCClient* client;
}

static NSString* const CONFIG_FILE_CONTENT = @""
"[general]\n"
"anon-access = none\n"
"auth-access = write\n"
"password-db = passwd\n"
"realm = Test Realm\n"
;

static NSString* const PASSWORDS = @""
"[users]\n"
"%@ = secret\n"
;



- (void)setUp
{
    [super setUp];
    NSString* cwd = [[NSFileManager defaultManager] currentDirectoryPath];
    if ([cwd isEqualToString: @"/"])
    {
        [[NSFileManager defaultManager] changeCurrentDirectoryPath: NSTemporaryDirectory()];
    }
    cwd = [[NSFileManager defaultManager] currentDirectoryPath];
    NSLog(@"CWD = %@", cwd);
    NSString* output = [fileHelper outputOfTask: @"/bin/ls" withArguments: @[ @"-la"]];
    NSLog(@"%@", output);
    output = [fileHelper outputOfTask: @"/usr/bin/whoami" withArguments: @[]];
    NSLog(@"%@", output);

    if (fileHelper == nil)
    {
        fileHelper = [[SFCFileSetUpHelper alloc] initWithFolder: [self className]];
    }
    [fileHelper resetFiles];
    [fileHelper addFileToRepository: @"test.txt" content: @"line 1\n"];
    [fileHelper updateFileInRepository: @"test.txt" appendContent: @"line 2\n"];
    [fileHelper writeRepoConfig: CONFIG_FILE_CONTENT];
    [fileHelper writePasswords: [NSString stringWithFormat: PASSWORDS, NSUserName()]];
    [fileHelper startServer];
    NSError* error;
    client = [[SFCClient alloc] initWithError: &error];
    if (client == nil)
    {
        @throw [NSException exceptionWithName: @"SFCAuthTests"
                                       reason: [NSString stringWithFormat: @"%@", error]
                                     userInfo: nil];
    }
    else
    {
        SFCTestUserNameProvider* userNameProvider = [[SFCTestUserNameProvider alloc] init];
        [client addAuthenticationProvider: userNameProvider];
    }
}

- (void)tearDown
{
    [fileHelper stopServer];
    [super tearDown];
}

/*!
 *    @brief Testing anonymous checkout against a server that requires 
 *           authentication.
 *
 *    This checkout should fail.  I haven't configured any auth, but the repo
 *    is set up for no anon access.
 */
-(void) testAnonCheckout
{
    SFCRevNum resultRevision;
    NSError* error;
    bool success = [client checkoutURL: [fileHelper svnServeURI]
                                  path: [fileHelper wcPath]
                           pegRevision: nil
                              revision: nil
                                 depth: SFC_DEPTH_INFINITY
                       ignoreExternals: true
          allowUnversionedObstructions: false
                        resultRevision: &resultRevision
                                 error: &error];
    XCTAssertFalse(success, @"Checkout should not have succeeded");
    NSLog(@"%@", error);
}
/*!
 *    @brief Test simple credentials provider.
 *
 *    The simple credentials provider provides a username/password pair.
 */
-(void) testSimple
{
    SFCRevNum resultRevision;
    NSError* error;
    SFCTestSimpleProvider* simpleProvider = [[SFCTestSimpleProvider alloc] initWithPassword: @"secret"];
    [client addAuthenticationProvider: simpleProvider];

    bool success = [client checkoutURL: [fileHelper svnServeURI]
                                  path: [fileHelper wcPath]
                           pegRevision: nil
                              revision: nil
                                 depth: SFC_DEPTH_INFINITY
                       ignoreExternals: true
          allowUnversionedObstructions: false
                        resultRevision: &resultRevision
                                 error: &error];
    XCTAssertTrue(success, @"Checkout failed with error %@", error);

}

@end
